package com.ambercrm.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import com.ambercrm.R;
import com.ambercrm.common.StateLayout;
public abstract class BaseDrawerTitleActivity extends BaseActivity {
    protected DrawerLayout mDrawerLayout;
    RelativeLayout mTitleLayout;  // 整个title的layout
    ImageView mBackBt;   // 返回按钮
    TextView mTitleText; // title中间的文本

    // 搜索、筛选、排序按钮
    ImageView mSearchBt, mFilterBt, mSortBt;

    View mContentView; // 用户填入的content view
    LayoutInflater mInflater;
    RelativeLayout mContentContainer;    // 中间内容的layout
    LinearLayout mDrawerContentContainer; // 放抽屉View的layout

    protected StateLayout mStateLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_base_drawer_title);
        mDrawerLayout = findViewById(R.id.drawerlayout);
        mTitleLayout = findViewById(R.id.base_title);
        mBackBt = findViewById(R.id.base_fragment_back_bt);
        mTitleText = findViewById(R.id.title_text);
        mSearchBt = findViewById(R.id.base_iv_search);
        mFilterBt = findViewById(R.id.base_iv_filter);
        mSortBt = findViewById(R.id.base_iv_sort);
        mContentContainer = findViewById(R.id.content_container);
        mDrawerContentContainer = findViewById(R.id.drawer_container);
        mStateLayout = findViewById(R.id.state_layout);

        mInflater = LayoutInflater.from(this);
        // 初始化内容部分
        int contentId = getContentLayoutId();
        if (contentId != 0) {
            mContentView = mInflater.inflate(contentId, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            mContentContainer.addView(mContentView, params);
            initContentView(mContentView);
        }

        OnTitleButtonClick onTitleButtonClick = new OnTitleButtonClick();
        mSearchBt.setOnClickListener(onTitleButtonClick);
        mFilterBt.setOnClickListener(onTitleButtonClick);
        mSortBt.setOnClickListener(onTitleButtonClick);
        mBackBt.setOnClickListener(onTitleButtonClick);
    }

    // 传入内容部分的布局
    protected abstract int getContentLayoutId();

    // 初始化内容部分
    protected abstract void initContentView(View v);

    class OnTitleButtonClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == mSearchBt) {
                searchClick(v);
            }
            else if (v == mFilterBt) {
                filterClick(v);
            }
            else if (v == mSortBt) {
                sortClick(v);
            }
            else if (v == mBackBt) {
                finishActivity();
            }
        }
    }

    // 想要控制title上这3个按钮，只要把按钮显示出来，然后重写这3个点击事件方法就好了
    protected void searchClick(View v) {

    }

    protected void filterClick(View v) {

    }

    protected void sortClick(View v) {

    }

    // 添加抽屉View
    protected void setDrawerView(View v) {
        mDrawerContentContainer.removeAllViews();
        mDrawerContentContainer.addView(v);
    }

    // 给抽屉部分设置fragment
    protected void setDrawerFragment(Fragment fragment) {
        mDrawerContentContainer.removeAllViews();
        getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container, fragment).commit();
    }
}
